<template>
  <div class="login-container">
    <div class="login-card">
      <div class="image-container">
        <!-- 灰色阴影块，替代图片容器 -->
        <img
          src="../assets/111.png"
          alt="登录图片"
          width="380px"
          height="380px"
        />
      </div>
      <div class="form-container">
        <div class="aaa">
          <p>用户登录</p>
          <div @click="$router.push('/register')" class="zhanghao">
            <span class="no-account">没有账号</span>，<span
              class="click-register"
              >点击注册</span
            >
          </div>
        </div>
        <div class="hua"></div>

        <el-form
          :model="loginForm"
          ref="loginForm"
          :rules="rules"
          label-width="0"
        >
          <el-form-item prop="account">
            <el-input
              v-model="loginForm.phone_num"
              placeholder="手机号/邮箱"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="loginForm.password"
              placeholder="输入密码"
              prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>
          <div class="wangji">
            <div href="#" class="ziti">忘记密码?</div>
          </div>
          <div class="shouce">
            <el-checkbox v-model="loginForm.remember">
              <span class="no-account1">同意并愿意遵守</span>，<span
                class="click-register1"
                >《****服务协议》</span
              ></el-checkbox
            >
          </div>
          <div class="anniu">
            <el-form-item class="anniu">
              <el-button type="primary" @click="handleLogin" round
                >立即登录</el-button
              >
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

// import axios from "../api/axios";
import api from '../api';
export default {
  name: "StudentLogin",
  data() {
    return {
      loginForm: {
        phone_num: "",
        password: "",
        remember: false,
        user_type: "student", // 默认用户类型为学生
        registeredUsers: [],
      },
      rules: {
        phone_num: [
          { required: true, message: "请输入手机号/邮箱", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  created() {
    // 解析路由参数并存入组件的数据
    this.registeredUsers = JSON.parse(
      this.$route.query.registeredUsers || "[]"
    );
    // 输出到控制台
    console.log("Registered Users:", this.registeredUsers);
  },
  methods: {
    handleLogin() {
      if (!this.loginForm.remember) {
        this.$message.error("请勾选同意使用该账号");
        return;
      }

      // 设置用户类型
      this.loginForm.user_type = this.loginForm.isStudent
        ? "student"
        : "teacher";
      // 表单验证
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          const { phone_num, password, user_type } = this.loginForm;
          api.
          Login(phone_num,password,user_type) 
            .then((response) => {
              const { status, msg, data } = response.data;
              if (status === 1) {
                // this.registeredUsers.push(this.registerForm.username);
                this.$message.success(msg);
                localStorage.setItem("auth", true);
                localStorage.setItem("token", data.token);
                localStorage.setItem("user_type", data.user_type);
                localStorage.setItem("userid", data.id);
                localStorage.setItem("semester_id", data.semester_id);
                localStorage.setItem("tech_id", data.tech_id);
                localStorage.setItem("start_time", data.start_time);
                localStorage.setItem("course_id", data.course_id);
                localStorage.setItem("end_time", data.end_time);
                localStorage.setItem("semester_name", data.semester_name);
                localStorage.setItem("semester_intro", data.semester_intro);
                localStorage.setItem("stu_cnt", data.stu_cnt);
                localStorage.setItem("semester_code", data.semester_code);
                localStorage.setItem("semester_status", data.semester_status);
                this.$router.push({
                  path: "/",
                });
              } else {
                this.$message.error(msg);
              }
            })
            .catch((error) => {
              console.error(error);
              this.$message.error("登录失败，请稍后再试");
            });
        } else {
          const errors = this.$refs.loginForm.fields.filter(
            (field) => !field.validateState.valid
          );
          if (errors.length > 0) {
            this.$message.error("请完善表单信息");
          } else {
            this.$message.error("发生错误");
          }
        }
      });
    },
  },
};
</script>
<!-- 
<style src="../css/login.css"></style> -->
<style scoped>
p {
  font-size: 24px;
  margin-top: 0px;
  height: 30px;
  color: rgba(0, 0, 0, 1);
}

.login-card {
  animation: swing 1.5s ease-in-out infinite,
    swing-glow 1.5s ease-in-out infinite;
}

/* 鼠标悬停时停止动画 */
.login-card:hover {
  animation-play-state: paused;
}
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #409eff;
}

.no-account1 {
  color: rgba(57, 61, 66, 1);
  font-size: 12px;
  align-items: right;
  margin-top: 0px;
}
.click-register1 {
  height: 30px;
  color: #409eff; /* 点击注册显示为蓝色 */
  font-size: 12px;
}

.no-account {
  color: rgba(57, 61, 66, 1);
  font-size: 14px;
  align-items: right;
  height: 30px;
}
.click-register {
  height: 30px;
  color: #409eff; /* 点击注册显示为蓝色 */
  font-size: 14px;
  cursor: pointer;
}
.el-link {
  display: inline-block; /* 将 el-link 设置为行内块级元素 */
}

.aaa {
  display: flex;
  flex-direction: row;
  align-items: right;
  width: 310px;
  height: 50px;
  margin-left: 20px;
}

.anniu {
  margin-top: 0;
}

.shouce {
  margin-left: 80px;
  margin-bottom: 20px;
  margin-top: 0px;
  display: flex;
}

.zhanghao {
  display: flex;
  margin-top: 15px;
  margin-left: 60px;
  height: 30px;
  cursor: pointer;
}
.hua {
  border-top: 108px;
}

.hua1 {
  border-top: 308px;
}
.wangji {
  font-size: 12px;
  display: flex;
  color: rgba(24, 144, 255, 1);
  margin-left: 110px;
}
.switch {
  position: relative;
  display: inline-block;
  width: 80px;
  height: 22px;
  left: 100px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.25);
  transition: 0.4s;
  border-radius: 34px; /* 设置滑块的圆角 */
}
.ziti {
  font-size: 12px;
  margin-left: 150px;
  color: rgba(24, 144, 255, 1);
  margin-top: 10px;
}
.slider.round {
  border-radius: 34px;
}

.zhece {
  font-size: 14px;
  font-weight: 400;
  letter-spacing: 0px;
  line-height: 20.27px;
  color: rgba(57, 61, 66, 1);
  border-top: 50px;
}
.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: 0.4s;
  border-radius: 50%; /* 使滑块按钮成为圆形 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); /* 可选：添加阴影效果 */
}

input:checked + .slider {
  background-color: #2196f3;
}

input:checked + .slider:before {
  transform: translateX(58px); /* 设置滑块按钮移动距离 */
}
.inner-label {
  font-size: 12px;
  font-weight: 400;

  line-height: 22px;
  color: rgba(255, 255, 255, 1);
  text-align: right;
  vertical-align: top;
  margin-left: 10px;
}
.inner-label1 {
  font-size: 12px;
  font-weight: 400;

  line-height: 22px;
  color: rgba(255, 255, 255, 1);
  text-align: right;
  vertical-align: top;
  margin-right: 10px;
}

.login-card {
  display: flex;
  width: 800px;
  height: 400px;
  background: white;
  border-radius: 30px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.image-container {
  flex: 2;
  background-color: #fff8f2;
  border-top-left-radius: 30px;
  border-bottom-left-radius: 30px;
}

.form-container {
  flex: 1;
  padding: 40px 40px;
}

.el-input {
  width: 90%;
  margin-top: 10px;
  margin-left: 20px;
}

.el-button {
  width: 100%;
  background-color: #409eff;
  border: none;
  margin-top: -20px;
}

.el-link {
  display: block;
  text-align: center;
  margin-top: 10px;
}

.el-checkbox {
  margin-bottom: 20px;
  display: block;
  text-align: center;
  margin-top: 10px;
  margin-left: -70px;
}
</style>
